/* Exercise 4.2 LED driver */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly //A value for each bit in PORTA. We can feed in the number of the LED and it will give us the value to put into A const unsigned char enable [4] = { 1, 2, 4, 8 } ; //These are the patterns for the LEDs which were worked out from the datasheet. //Note that to light a LED the bit on PORTB must be low I can use this array to convert from a digit to the 7 segments needed const unsigned char patterns [10] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x83, 0xf8, 0x80, 0x98 } ; // 0 1 2 3 4 5 6 7 8 9 void setup_hardware (void) { ANSELA = 0x00; //Set PORTA to Digital Mode - Defaults to analogue mode ANSELB = 0x00; //Set PORTB to Digital Mode - Defaults to analogue mode OPTION_REG = 0xC0; TRISA = 0xF0; //Set bits 0-3 PORTA to be configured as an output TRISB = 0x00; //Set all bits of PORTB to be configured as an output } //Performs a delay so we can hold the display for us to read it void delay ( unsigned char size ) { unsigned char i ; for ( i=0 ; i < size ; i= i + 1 ) ; } //This function takes two parameters, the value to be displayed and the number of the LED unit to use void display ( unsigned char digit, unsigned char pos ) { LATB = patterns[digit]; //Set the pattern on the LED LATA = enable[pos]; //Turn on the required LED unit delay(10); LATA = 0; } int main (void) { setup_hardware(); while (1) //Loop forever { display(0, 0); //Display the number 0 on segment 0 display(1, 1); //Display the number 1 on segment 1 display(2, 2); //Display the number 2 on segment 2 display(3, 3); //Display the number 3 on segment 3 } return 0; }